package com.mge.ae.views.filetree;

import java.io.File;

import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.part.ViewPart;
/**
 * 资源导航视图，展示工作空间下的资源结构
 * @author wangbin
 *
 */
public class FileTreeView extends ViewPart {

	public static final String ID = "com.mag.ae.views.filetree.filetreeview";
	
	public FileTreeView() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		final TreeViewer treeViewer = new TreeViewer(parent);
		treeViewer.setContentProvider(new FileTreeContentProvider());
		treeViewer.setLabelProvider(new FileTreeLabelProvider());
		treeViewer.setInput(new File("d:/"));
		treeViewer.addDoubleClickListener(new IDoubleClickListener() {
			
			@Override
			public void doubleClick(DoubleClickEvent event) {
				// TODO Auto-generated method stub
				IStructuredSelection is = (IStructuredSelection)event.getSelection();
				Object obj = is.getFirstElement();
				TreeItem[] items = treeViewer.getTree().getSelection();
				if(items == null || items.length == 0){
					return;
				}
				if(items[0].getExpanded()){
					items[0].setExpanded(false);
				}else{
					treeViewer.expandToLevel(obj, 1);
				}
			}
		});
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}
